package com.pktechan.api.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.pktechan.api.base.MiniProgramGlobalVar;
import com.pktechan.api.dao.SendMessageMapper;
import com.pktechan.api.dto.MiniSubscribeMessage;
import com.pktechan.api.entity.WxMsgLog;
import com.pktechan.api.sendMiniMessage.bean.*;
import com.pktechan.api.service.SendMessageService;
import com.pktechan.api.service.WxMsgLogService;
import com.pktechan.api.utils.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @program: cloudfloating
 * @description: 发送消息相关功能实现
 * @author: qx.zhang
 * @create: 2021-03-23 09:25
 **/
@Service("sendMessageService")
@Transactional
public class SendMessageServiceImpl implements SendMessageService {

    private static final Logger logger = LoggerFactory.getLogger(SendMessageServiceImpl.class);

    @Autowired
    private WxMsgLogService wxMsgLogService;

    @Autowired
    private SendMessageMapper sendMessageMapper;

    /**
     * 发送小程序订阅消息
     *
     * @param accessToken
     * @param miniSubscribeMessage
     */
    @Override
    public void sendMiniSubscribeMessage(String accessToken, MiniSubscribeMessage miniSubscribeMessage) {
        String sendMiniSubscribeMessageUrl = MiniProgramGlobalVar.URL_SUBSCRIBE_MESSAGE_SEND;
        sendMiniSubscribeMessageUrl = sendMiniSubscribeMessageUrl.replace("ACCESS_TOKEN", accessToken);
        logger.debug("accessToken：" + accessToken);
        String reqJson = miniSubscribeMessage.toJSON();
        logger.debug("请求发送小程序订阅消息json是：" + reqJson);
        //微信接口不支持http请求，调用接口使用https
        JSONObject respJson = HttpUtil.doPostSSL(sendMiniSubscribeMessageUrl, reqJson);
        logger.debug("返回json是：" + respJson);
        if (respJson != null) {
            //保存微信消息推送日志
            WxMsgLog wxMsgLog = new WxMsgLog();
            int errorCode = respJson.getInteger("errcode");
            String errorMessage = respJson.getString("errmsg");
            String msgId = respJson.getString("msgid");
            if (errorCode != 0) {
                logger.debug("发送小程序订阅消息失败：错误代码：" + errorCode + ",错误原因：" + errorMessage);
                wxMsgLog.setStatus(-1);
//                throw new CustomException("发送小程序订阅消息失败：错误代码：" + errorCode + ",错误原因：" + errorMessage);
            } else {
                wxMsgLog.setStatus(1);
            }
            wxMsgLog.setMsgId(msgId);
            wxMsgLog.setMpId(miniSubscribeMessage.getMpId());
            wxMsgLog.setTempId(miniSubscribeMessage.getTemplateId());
            wxMsgLog.setOpenId(miniSubscribeMessage.getToUser());

            wxMsgLog.setErrorCode(errorCode);
            wxMsgLog.setErrorMsg(errorMessage);
            wxMsgLogService.save(wxMsgLog);
        }
    }

    /**
     * 获取订单配送消息通知
     *
     * @param tradeOrderId
     * @return
     */
    @Override
    public CustomerOrderDeliveryBeginMessage getCustomerOrderDeliveryMessage(Integer tradeOrderId) {
        return sendMessageMapper.getCustomerOrderDeliveryMessage(tradeOrderId);
    }

    /**
     * 获取订单配送消息通知
     *
     * @param tradeOrderId
     * @return
     */
    @Override
    public CustomerDeliveryCompleteMessage getCustomerOrderDeliveryCompleteMessage(Integer tradeOrderId) {
        return sendMessageMapper.getCustomerOrderDeliveryCompleteMessage(tradeOrderId);
    }

    /**
     * 获取会员绑定成功消息通知
     *
     * @param custId
     * @return
     */
    @Override
    public CustomerBindingSuccessMessage getCustomerBindingSuccessMessage(Integer custId) {
        return sendMessageMapper.getCustomerBindingSuccessMessage(custId);
    }

    /**
     * 获取业绩消息通知
     *
     * @param tradeOrderId
     * @return
     */
    @Override
    public ReferencesOrderMessage getReferencesOrderMessage(Integer tradeOrderId) {
        return sendMessageMapper.getReferencesOrderMessage(tradeOrderId);
    }

    /**
     * 获取订单转让通知
     *
     * @param tradeOrderId
     * @return
     */
    @Override
    public DistributorOrderTransferApplyMessage getDistributorOrderTransferApplyMessage(Integer tradeOrderId) {
        return sendMessageMapper.getDistributorOrderTransferApplyMessage(tradeOrderId);
    }

    /**
     * 查询配送商发货提醒消息通知
     *
     * @param tradeOrderId
     * @return
     */
    @Override
    public DistributorOrderDeliverRemindMessage getDistributorOrderDeliverRemindMessage(Integer tradeOrderId) {
        return sendMessageMapper.getDistributorOrderDeliverRemindMessage(tradeOrderId);
    }

    /**
     * 查询厂家接收-客户-完成支付-订单支付成功通知
     *
     * @param tradeOrderId
     * @return
     */
    @Override
    public FactoryOrderPayMessage getFactoryOrderPayMessage(Integer tradeOrderId) {
        return sendMessageMapper.getFactoryOrderPayMessage(tradeOrderId);
    }

    /**
     * 厂家接收-配送商-完成配送-配送完成通知
     *
     * @param tradeOrderId
     * @return
     */
    @Override
    public FactoryDeliveryCompleteMessage getFactoryDeliveryCompleteMessage(Integer tradeOrderId) {
        return sendMessageMapper.getFactoryDeliveryCompleteMessage(tradeOrderId);
    }

    /**
     * 厂家接收-配送商-已接单-订单派送提醒
     *
     * @param tradeOrderId
     * @return
     */
    @Override
    public FactoryOrderDeliveryMessage getFactoryOrderDeliveryMessage(Integer tradeOrderId) {
        return sendMessageMapper.getFactoryOrderDeliveryMessage(tradeOrderId);
    }

    /**
     * 厂家接收-配送商-订单转让确认-订单配送通知
     *
     * @param tradeOrderId
     * @return
     */
    @Override
    public FactoryOrderTransferConfirmMessage getFactoryOrderTransferConfirmMessage(Integer tradeOrderId) {
        return sendMessageMapper.getFactoryOrderTransferConfirmMessage(tradeOrderId);
    }

    /**
     * 厂家接收-推荐人-佣金提现-提现申请通知
     *
     * @param commissionWithdrawFlowId
     * @return
     */
    @Override
    public FactoryReferrerCommissionApplyMessage getFactoryReferrerCommissionApplyMessage(Integer commissionWithdrawFlowId) {
        return sendMessageMapper.getFactoryReferrerCommissionApplyMessage(commissionWithdrawFlowId);
    }

    /**
     * 厂家接收-配送商-运费提现-提现申请通知
     *
     * @param custCarriageFlowId
     * @return
     */
    @Override
    public FactoryCarriageApplyMessage getFactoryCarriageApplyMessage(Integer custCarriageFlowId) {
        return sendMessageMapper.getFactoryCarriageApplyMessage(custCarriageFlowId);
    }

    /**
     * 推荐人接收-客户-完成支付-推荐人业绩待确认-好友下单成功通知
     *
     * @param tradeOrderId
     * @return
     */
    @Override
    public ReferencesOrderPayMessage getReferencesOrderPayMessage(Integer tradeOrderId) {
        return sendMessageMapper.getReferencesOrderPayMessage(tradeOrderId);
    }
}
